package com.yiqi.zhiyuan.feature.mine.view;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.DensityUtil;
import com.suozhang.framework.utils.logger.Logger;
import com.suozhang.framework.widget.EmptyView;
import com.suozhang.framework.widget.dialog.NormalDialog;
import com.suozhang.framework.widget.dialog.OnBtnClickL;
import com.yiqi.zhiyuan.R;
import com.yiqi.zhiyuan.common.utils.AppUtil;
import com.yiqi.zhiyuan.common.widget.NoCrashLinearLayoutManager;
import com.yiqi.zhiyuan.entity.vo.CarVO;
import com.yiqi.zhiyuan.feature.certification.view.AddVehicleActivity;
import com.yiqi.zhiyuan.feature.certification.view.VehicleStateActivity;
import com.yiqi.zhiyuan.feature.mine.contract.CarContract;
import com.yiqi.zhiyuan.feature.mine.dependencies.register.CarPresenterModule;
import com.yiqi.zhiyuan.feature.mine.dependencies.register.DaggerCarComponent;
import com.yiqi.zhiyuan.feature.mine.view.adapter.VehicleAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: ZhiYuan
 * @Package: com.yiqi.zhiyuan.feature.mine.view
 * @ClassName: MyVehicleActivity
 * @Description: java类作用描述
 * @Author:
 * @CreateDate: 2019/3/12 18:22
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/12 18:22
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class MyVehicleActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, CarContract.View, View.OnClickListener, CompoundButton.OnCheckedChangeListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rcy_vehicle_list)
    RecyclerView mRvVehicle;
    @BindView(R.id.btn_car_edit)
    TextView mBtnCarEdit;
    @BindView(R.id.cbx_car_edit_all)
    CheckBox mCbxCarEditAll;
    @BindView(R.id.view_edit)
    LinearLayout mViewEdit;
    private EmptyView mEmptyView;
    private VehicleAdapter mAdapter;

    private boolean isEdit = false;
    private int mSelectPos = -1;

    @Inject
    CarContract.Presenter mPresenter;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_my_vehicle;
    }

    @Override
    protected void initInjector() {
        DaggerCarComponent.builder().carPresenterModule(new CarPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initAdapter();
        initToolBar(mToolbar, getString(R.string.vehicle_manage_title), true, true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.getDiverCarList();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        mCbxCarEditAll.setOnCheckedChangeListener(this);
    }

    private void initAdapter() {
        mEmptyView = new EmptyView(mRvVehicle);
        mRvVehicle.setLayoutManager(new NoCrashLinearLayoutManager(this));
        mRvVehicle.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(DensityUtil.dp2px(10)));
        mAdapter = new VehicleAdapter();
        mAdapter.bindToRecyclerView(mRvVehicle);
        mAdapter.addFooterView(getFooter());
        mAdapter.setHeaderFooterEmpty(false, true);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
        mAdapter.setCheckListener(listener);
    }

    private List<String> stringList = new ArrayList<>();
    private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                CarVO item = (CarVO) buttonView.getTag();
                if (isChecked) {
                    if (item.getIsUse() == 0) {
                        stringList.add(item.getId() + "");
                    }
                } else {
                    if (stringList.contains(item)) {
                        stringList.remove(item);
                    }
                }
            }
            stringList = new ArrayList<>(new HashSet(stringList));

        }
    };

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void showDiverCarListSuccess(List<CarVO> carVOS) {
        for (int i = 0; i < carVOS.size(); i++) {
            if (carVOS.get(i).getIsUse() == 1) {
                mAdapter.checkState(i);
            }
        }
        mAdapter.setNewData(carVOS);
    }

    @Override
    public void showDiverCarListError(String errorMsg) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(errorMsg));
    }

    @Override
    public void showDiverCarEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
    }

    @Override
    public void showOrderQuickSetupSuccess() {
        mPresenter.getDiverCarList();
    }

    @Override
    public void showOrderQuickSetupError(String errorMsg) {
        showMsg(errorMsg);
    }

    @Override
    public void showDeleteCarSuccess() {
        isEdit = false;
        mBtnCarEdit.setText(isEdit ? "完成" : "编辑");
        mViewEdit.setVisibility(isEdit ? View.VISIBLE : View.GONE);
        mAdapter.setCheck(isEdit);
        mPresenter.getDiverCarList();
    }

    @Override
    public void showDeleteCarError(String errorMsg) {
        showMsg(errorMsg);
    }

    private View getFooter() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.footer_add_car, null);
        view.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, AddVehicleActivity.class);
        intent.putExtra("is_first", false);
        startActivity(intent);

    }

    @OnClick(R.id.btn_car_edit)
    public void onViewEditClicked() {
        mAdapter.setCheckAll(false);
        mCbxCarEditAll.setChecked(false);
        if (isEdit) {
            isEdit = false;
        } else {
            isEdit = true;
        }
        mBtnCarEdit.setText(isEdit ? "完成" : "编辑");
        mViewEdit.setVisibility(isEdit ? View.VISIBLE : View.GONE);
        mAdapter.setCheck(isEdit);
    }


    /**
     * 删除选中车辆
     */
    @OnClick(R.id.btn_car_delete)
    public void onViewDeleteClicked() {
        Logger.d("onCheckedChanged-->" + stringList.size() + "");
        if (stringList.size() > 0) {
            initDialog();
        } else {
            showMsg("请先选择车辆");
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        buttonView.setText(isChecked ? "取消全选" : "全选");
        mAdapter.setCheckAll(isChecked);
        stringList.clear();
        if (isChecked) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                CarVO carVO = mAdapter.getItem(i);
                if (carVO.getIsUse() == 0) {
                    stringList.add(carVO.getId() + "");
                }
            }
        } else {
            stringList.clear();
        }

        Logger.d("onCheckedChanged-->" + stringList.size() + "");
    }

    private void initDialog() {
        NormalDialog dialog = new NormalDialog(this);
        dialog.content("是否删除选中车辆？");
        dialog.btnNum(2);
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                mPresenter.deleteCarInDriver(AppUtil.listToString(stringList));
            }
        });
        dialog.show();

    }



    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        mSelectPos = position;
        switch (view.getId()) {
            case R.id.view_show_used:
                if (!isEdit) {
                    mPresenter.orderQuickSetup(Integer.valueOf(mAdapter.getItem(position).getId()));
                }
                break;
            case R.id.tv_vehicle_adopt:
                if (mAdapter.getItem(position).getIsPass() == 1) {//还在审核中
//                    showMsg("");
                } else {
                    Intent intent = new Intent(this, VehicleStateActivity.class);
                    intent.putExtra("car_info", mAdapter.getItem(position));
                    intent.putExtra("is_first", false);
                    startActivity(intent);
                }
                break;
        }
    }
}
